﻿#include "VMineBlink.h"
#include "ui_VMineBlink.h"
#include <qdebug.h>
#include <QMessageBox>

static VMineBlink* pdlg = nullptr;
jsValue JS_CALL dlg_set_text(jsExecState es)
{
    jsValue v = jsArg(es, 0);
    pdlg->_text = jsToString(es, v);
    return jsUndefined();
}

jsValue JS_CALL dlg_accept(jsExecState es)
{
	pdlg->accept();
	return jsUndefined();
}

jsValue JS_CALL dlg_reject(jsExecState es)
{
	pdlg->reject();
	return jsUndefined();
}

void alert(wkeWebView webView, void* param, const wkeString msg)
{
    QMessageBox::question(pdlg, "信息", wkeGetString(msg), "确定");
}

VMineBlink::VMineBlink(QWidget *parent, QString path)
	: QDialog(parent)
{
	ui = new Ui::VMineBlink();
	ui->setupUi(this);
	pdlg = this;

    webView= wkeCreateWebWindow(WKE_WINDOW_TYPE_CONTROL, (HWND)this->winId(), 0, 0, this->width(), this->height());
    wkeShowWindow(webView, TRUE);
    wkeLoadFile(webView, path.toUtf8());
	this->installEventFilter(this);
    jsBindFunction("setText", dlg_set_text,1);
	jsBindFunction("accept", dlg_accept, 0);
	jsBindFunction("reject", dlg_reject, 0);
	wkeOnAlertBox(webView, &alert, nullptr);
}

VMineBlink::~VMineBlink()
{
    delete ui;
}

bool VMineBlink::eventFilter(QObject *obj, QEvent *e)
{
    if(e->type() == QEvent::Resize){
            wkeResize(webView, this->width(), this->height());
    }
    return QDialog::eventFilter(obj, e);
}
